package com.alexander.cloundexplorer.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;

import com.alexander.cloundexplorer.R;
import com.alexander.cloundexplorer.bean.DirectoryInfo;
import com.alexander.cloundexplorer.bean.params.RenameDirParams;
import com.alexander.cloundexplorer.bean.params.User;
import com.alexander.cloundexplorer.bean.params.RenameFileParams;
import com.alexander.cloundexplorer.bean.response.FileInfoResponse;
import com.alexander.cloundexplorer.util.Constants;
import com.alibaba.fastjson.JSON;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * @author 14014
 * 重命名文件或者文件夹
 */
public class RenameActivity extends BaseActivity
{
    @BindView(R.id.newName_EditText)
    EditText newNameEditText;
    private String isFile = "0";
    private DirectoryInfo directoryInfo;
    private FileInfoResponse fileInfoResponse;
    private User user;
    private String oldName;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rename);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        isFile = intent.getStringExtra("isFile");
        if(isFile.equals("1"))
        {
            fileInfoResponse = (FileInfoResponse) intent.getSerializableExtra("info");
            oldName = fileInfoResponse.getFileName().concat(fileInfoResponse.getFileNameSameNo());
        }else
        {
            directoryInfo = (DirectoryInfo) intent.getSerializableExtra("info");
            oldName = directoryInfo.getDirectoryName();
        }
        user = (User) intent.getSerializableExtra("user");
        newNameEditText.setText(oldName);
        newNameEditText.setSelection(0, oldName.length());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.menu_options_rename, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case R.id.sure:
                System.out.println("重命名重命名重命名");
                String newName = newNameEditText.getText().toString();
                if(!TextUtils.isEmpty(newName))
                {
                    if(newName.equals(oldName))
                    {
                        System.out.println("文件名没有变化,不发起请求");
                    }
                    else
                    {
                        if(isFile.equals("1"))
                        {
                            RenameFileParams renameFileParams = new RenameFileParams();
                            renameFileParams.setAccount(user.getPhone());
                            renameFileParams.setPassword(user.getPassword());
                            renameFileParams.setFileId(String.valueOf(fileInfoResponse.getId()));
                            renameFileParams.setFatherDirectoryId(fileInfoResponse.getFatherDirectoryId());
                            renameFileParams.setNewOriginFileName(newName.concat(fileInfoResponse.getFile_postfix()));
                            String jsonStr = JSON.toJSONString(renameFileParams);
                            String url = "/fileController/renameFile";
                            rename(url, jsonStr);
                        }else
                        {
                            RenameDirParams renameDirParams = new RenameDirParams();
                            renameDirParams.setAccount(user.getPhone());
                            renameDirParams.setPassword(user.getPassword());
                            renameDirParams.setDirId(String.valueOf(directoryInfo.getId()));
                            renameDirParams.setFatherDirectoryId(directoryInfo.getFatherID());
                            renameDirParams.setNewOriginFileName(newName);
                            String jsonStr = JSON.toJSONString(renameDirParams);
                            String url = "/fileController/renameDir";
                            rename(url, jsonStr);
                        }
                    }
                }
                else
                {
                    showTip("请输入文件名");
                }
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private void rename(String url, String params)
    {
        OkHttpUtils
                .post()
                .url(Constants.IP.concat(url))
                .addParams("params", params)
                .build()
                .execute(new StringCallback()
                {
                    @Override
                    public void onError(Call call, Exception e, int id)
                    {
                        showTip(e.getMessage());
                    }
                    @Override
                    public void onResponse(String response, int id)
                    {
                        showTip(response);
                        if(response.contains("800") || response.contains("1800"))
                        {}
                        else if(response.contains("801") || response.contains("1801"))
                        {}
                    }
                    @Override
                    public void onAfter(int id)
                    {
                        super.onAfter(id);
                        finish();
                    }
                });
    }
}
